using DesktopWidgetApp.Core.Data;
using DesktopWidgetApp.Core.Services;
using DesktopWidgetApp.UI.Helpers;
using DesktopWidgetApp.Shared;
using System;
using System.Windows;
using System.Threading.Tasks;

namespace DesktopWidgetApp.UI.Services
{
    /// <summary>
    /// 应用程序服务类
    /// 负责管理应用程序的核心服务和生命周期
    /// </summary>
    public class AppService : IDisposable
    {
        private readonly StatusService _statusService;
        private readonly TaskService _taskService;
        private readonly IStatusRepository _statusRepository;
        private readonly ITaskRepository _taskRepository;
        private readonly DataSyncManager _dataSyncManager;
        private readonly StatusSyncHelper _statusSyncHelper;
        private readonly TrayIconManager _trayIconManager;
        private readonly Guid _currentUserId;
        private bool _isDisposed = false;

        /// <summary>
        /// 当前用户ID
        /// </summary>
        public Guid CurrentUserId => _currentUserId;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AppService()
        {
            try
            {
                // 初始化数据库和服务
                var databasePath = Core.Helpers.DatabaseHelper.GetDatabasePath();
                _statusRepository = new SqliteStatusRepository(databasePath);
                _statusService = new StatusService(_statusRepository);
                _taskRepository = new SqliteTaskRepository(databasePath);
                _taskService = new TaskService(_taskRepository);
                _statusSyncHelper = new StatusSyncHelper();
                _dataSyncManager = new DataSyncManager(_statusRepository, _statusSyncHelper);
                
                // 当前用户ID (这里使用临时ID，实际应用中应该从登录状态获取)
                _currentUserId = new Guid("12345678-1234-1234-1234-123456789012");
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化应用服务失败: " + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                throw;
            }
        }

        /// <summary>
        /// 初始化应用程序服务
        /// </summary>
        public async Task InitializeAsync()
        {
            try
            {
                // 初始化数据库
                await Core.Helpers.DatabaseHelper.InitializeDatabaseAsync();
                
                // 执行首次同步
                await _dataSyncManager.PerformFullSyncAsync(_currentUserId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("初始化应用服务异常: " + ex.Message);
                // 即使初始化失败，也继续运行应用程序
            }
        }

        /// <summary>
        /// 设置托盘图标管理器
        /// </summary>
        /// <param name="mainWindow">主窗口</param>
        public void SetTrayIconManager(Window mainWindow)
        {
            _trayIconManager = new TrayIconManager(mainWindow);
        }

        /// <summary>
        /// 获取状态服务
        /// </summary>
        public StatusService StatusService => _statusService;

        /// <summary>
        /// 获取状态同步辅助类
        /// </summary>
        public StatusSyncHelper StatusSyncHelper => _statusSyncHelper;

        /// <summary>
        /// 获取数据同步管理器
        /// </summary>
        public DataSyncManager DataSyncManager => _dataSyncManager;

        /// <summary>
        /// 获取托盘图标管理器
        /// </summary>
        public TrayIconManager TrayIconManager => _trayIconManager;

        /// <summary>
        /// 获取任务服务
        /// </summary>
        public TaskService TaskService => _taskService;

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if (!_isDisposed)
            {
                _trayIconManager?.Dispose();
                _isDisposed = true;
            }
        }
    }
}